#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<sys/un.h>
#include<arpa/inet.h>
#include<unistd.h>
#define MAXLINE 4096

int main(int argc, char** argv){
    int   sockfd, n;
    char  recvline[4096], sendline[4096];
    struct sockaddr_un  servaddr;


    if( (sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
        printf("create socket error: %s(errno: %d)\n", strerror(errno),errno);
        return 0;
    }

    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sun_family = AF_UNIX;
    memcpy(servaddr.sun_path, "/tmp/uds-tcp.sock",strlen("/tmp/uds-tcp.sock"));

    if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0){
        printf("connect error: %s(errno: %d)\n",strerror(errno),errno);
        return 0;
    }

    while (1) {
        printf("send msg to server: \n");
        memset(sendline,0,4096);
        scanf("%s", sendline);
        if( send(sockfd, sendline, strlen(sendline), 0) < 0){
            printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
            return 0;
        }
        memset(recvline, 0, 4096);
        n=recv(sockfd, recvline, 4096, 0);
        if(n<=0)
            break;
        if(strcmp(recvline, "exit")==0){
            printf("clost fd!\n");
            break;
        }else{
            printf("recv %s\n", recvline);
        }
    }

    close(sockfd);
    return 0;
}
